#include "Pause.h"
#include "Game.h"
#include "GamePlayState.h"
#include "BitmapFont.h"
#include "MainMenuState.h"
#include "AudioManager.h"


Pause::Pause(void)
{
	cursor = 0;
	pauseMenuRect.left = Game::GetInstance()->GetScreenWidth() / 2-200;
	pauseMenuRect.right = Game::GetInstance()->GetScreenWidth() / 2+200;
	pauseMenuRect.top = 50;
	pauseMenuRect.bottom = Game::GetInstance()->GetScreenHeight() -100;
	curSubMenu = NONE;


	ResumeSelectedID = -1;
	OptionSelectedID = -1;
	HelpSelectedID = -1;
	ExitSelectedID = -1;

}


Pause::~Pause(void)
{
}

void Pause::Update(float deltaTime)
{	 
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;

	if(dI->KeyPressed(DIK_ESCAPE) || dI->JoystickButtonPressedEx(6) || dI->JoystickButtonPressedEx(7))
	{
		switch(curSubMenu)
		{
		case NONE:
			GamePlayState::GetInstance()->Unpause();
			return;
			break;
		case OPTIONS:
			curSubMenu = NONE;
			optionsMenu.Exit();
			break;
		case HELP:
			curSubMenu = NONE;
			helpMenu.Exit();
			return;
			break;
		}
	}
	if(curSubMenu == NONE)
	{
		if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
		{
			controllerDelay = 0.17f;
			if(cursor > 0)
				--cursor;
			else
				cursor = 3;
		}
		else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
		{
			controllerDelay = 0.25f;
			if(cursor < 3)
				++cursor;
			else
				cursor = 0;
		}

		if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
		{
			switch(cursor)
			{
			case 0:
				GamePlayState::GetInstance()->Unpause();
				return;
				break;
			case 1:
				curSubMenu = OPTIONS;
				optionsMenu.Enter(this);
				return;
				break;
			case 2:
				curSubMenu = HELP;
				helpMenu.Enter(this);
				break;
			case 3:
				GamePlayState::GetInstance()->Unpause();
				AudioManager::GetInstance()->switchMusic("gameplay", "menus");
				AudioManager::GetInstance()->switchMusic("gameplay2", "menus");
				Game::GetInstance()->ChangeState(MainMenuState::GetInstance());
				return;
				break;
			}
			return;
		}
	}
	if(curSubMenu == OPTIONS)
	{
		optionsMenu.Update(deltaTime);
	}
	if (curSubMenu == HELP)
	{
		helpMenu.Update(deltaTime);
	}
}	 
void Pause::Render(void) const		
{	
	if(curSubMenu == NONE)
	{
		//D3D->DrawRect(pauseMenuRect,D3DCOLOR_ARGB(255,150,155,155));
		//D3D->DrawText(_T("PAUSED"),pauseMenuRect.left + (pauseMenuRect.right-pauseMenuRect.left)/2,pauseMenuRect.top,D3DCOLOR_ARGB(255,30,40,50));
		//BitmapFont::GetInstance()->Print("PAUSED", pauseMenuRect.left + (pauseMenuRect.right-pauseMenuRect.left)/2 -60.0f, (float)pauseMenuRect.top ,0, D3DCOLOR_ARGB(255,30,40,50));

		//RECT renderRect;
		//for(int curMenuItem = 0; curMenuItem < 4; ++curMenuItem)
		//{
		//	renderRect.left = pauseMenuRect.left + 50;
		//	renderRect.right = pauseMenuRect.right - 50;
		//	renderRect.top  = (pauseMenuRect.top + 45) + 60*curMenuItem +30*curMenuItem;
		//	renderRect.bottom= (pauseMenuRect.top + 120) + 60*curMenuItem + 30*curMenuItem;
		//	//D3D->DrawRect(renderRect, D3DCOLOR_ARGB(255,170,0,0));


		//Calculate the X and Y position to Draw
		int ScreenCenterX = Game::GetInstance()->GetScreenWidth() / 2;
		int ScreenCenterY = Game::GetInstance()->GetScreenHeight() / 2;

		int DrawXPos = ScreenCenterX - 208;
		int DrawYPos = ScreenCenterY - 274;
			switch(cursor)
			{

			case 0:
				{
					//BitmapFont::GetInstance()->Print("Resume", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
							
					if(ResumeSelectedID != -1)
						tM->Draw(ResumeSelectedID,DrawXPos,DrawYPos);
					break;	
				}
			case 1:		
				{
					//BitmapFont::GetInstance()->Print("Options", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
					
					if(OptionSelectedID != -1)
						tM->Draw(OptionSelectedID,DrawXPos,DrawYPos);
					break;	
				}
			case 2:		
				{
					//BitmapFont::GetInstance()->Print("Help", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
					
					if(HelpSelectedID != -1)
						tM->Draw(HelpSelectedID,DrawXPos, DrawYPos);
					break;
				}
			case 3:		
				{
					//BitmapFont::GetInstance()->Print("Exit", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
					
					if(ExitSelectedID != -1)
						tM->Draw(ExitSelectedID,DrawXPos, DrawYPos);
					break;
				}
			//}
		}

		//RECT selectedRect;
		//selectedRect.left = pauseMenuRect.left + 50;
		//selectedRect.right = pauseMenuRect.right - 50;
		//selectedRect.top  = (pauseMenuRect.top + 45) + 60*cursor +30*cursor;
		//selectedRect.bottom= (pauseMenuRect.top + 120) + 60*cursor + 30*cursor;	
		//D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);
	}
	else if(curSubMenu == OPTIONS)
	{
		optionsMenu.Render();
	}
	else if(curSubMenu == HELP)
	{
		helpMenu.Render();
	}
}	

void Pause::Enter(void)			
{	
	GameState::Enter();
	curSubMenu = NONE;

	ResumeSelectedID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\PauseResume.png")));
	OptionSelectedID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\PauseOptions.png")));
	HelpSelectedID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\PauseHelp.png")));
	ExitSelectedID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\PauseExit.png")));


}	
void Pause::Exit(void)				
{
	GameState::Exit();
}

void Pause::changeSubMenu(int newMenu)
{
	curSubMenu = (subMenu)newMenu;
}
